package com.yami.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.util.SendUtil;
import com.yami.shop.vo.ConversationRongVo;
import io.rong.RongCloud;
import io.rong.methods.conversation.Conversation;
import io.rong.models.conversation.ConversationModel;
import io.rong.models.response.ConversationNotificationResult;
import io.rong.models.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * 会话模块
 * 会话消息免打扰服务
 * docs: "http://www.rongcloud.cn/docs/server.html#conversation_notification"
 */
@Slf4j
@Service
public class RongYunConversationServiceImpl {
    @Autowired
    private RongCloud rongCloud;

    private Conversation Conversation;

    @PostConstruct
    public void init() {
        Conversation = rongCloud.conversation;
    }


    /**
     * 设置用户某会话接收新消息时是否进行消息提醒。
     * 设置用户某个会话屏蔽 Push
     *
     * @param conversationRongVo 会话信息 其中type(必传)
     * @return ResponseResult
     **/
    public ServerResponseEntity<String> mute(ConversationRongVo conversationRongVo) throws Exception {
        ConversationModel conversation = new ConversationModel().setType(conversationRongVo.getType()).setUserId(conversationRongVo.getUserId()).setTargetId(conversationRongVo.getTargetId());
        ResponseResult muteConversationResult = Conversation.mute(conversation);
        if (SendUtil.requestSuccess(muteConversationResult)) return ServerResponseEntity.success();
        throw new YamiShopBindException("设置会话免打扰消息失败：" + muteConversationResult.getErrorMessage());
    }

    /**
     * 查询会话消息免打扰方法。
     * 查询用户某一会话消息免打扰的设置状态。
     *
     * @param conversationRongVo 会话信息 其中type(必传)
     * @return isMuted 消息免打扰设置状态，0 表示为关闭，1 表示为开启。
     **/
    public ServerResponseEntity<String> get(ConversationRongVo conversationRongVo) {
        ConversationModel conversation = new ConversationModel().setType(conversationRongVo.getType()).setUserId(conversationRongVo.getUserId()).setTargetId(conversationRongVo.getTargetId());
        ConversationNotificationResult getMuteConversationResult;
        try {
            getMuteConversationResult = (ConversationNotificationResult) Conversation.get(conversation);
        } catch (Exception e) {
            return ServerResponseEntity.fail(ResponseEnum.EXCEPTION);
        }
        log.info("getMuteConversationResult:  " + getMuteConversationResult.toString());
        if (ObjectUtil.isNotNull(getMuteConversationResult)) {
            if (getMuteConversationResult.getCode() == 200) {
                return ServerResponseEntity.success(getMuteConversationResult.getIsMuted().toString());
            } else {
                return ServerResponseEntity.fail(getMuteConversationResult.getCode(), getMuteConversationResult.getErrorMessage());
            }
        }
        return ServerResponseEntity.fail(ResponseEnum.CLEAN_TOKEN);
    }

    /**
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/conversation/conversation.html#unmute
     * 解除消息免打扰
     */
    public ServerResponseEntity<String> unMute(ConversationRongVo conversationRongVo) throws Exception {
        ConversationModel conversation = new ConversationModel().setType(conversationRongVo.getType()).setUserId(conversationRongVo.getUserId()).setTargetId(conversationRongVo.getTargetId());
        ResponseResult muteConversationResult = Conversation.unMute(conversation);
        if (SendUtil.requestSuccess(muteConversationResult)) {
            return ServerResponseEntity.success();
        }
        throw new YamiShopBindException("解除免打扰失败:" + muteConversationResult.getErrorMessage());
    }
}
